package com.atguigu.myzhxy.controller;

import com.atguigu.myzhxy.pojo.Student;
import com.atguigu.myzhxy.service.StudentService;
import com.atguigu.myzhxy.util.MD5;
import com.atguigu.myzhxy.util.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * ClassName: StudentController
 * Package: com.atguigu.myzhxy.controller
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-01-12
 * Time: 15:41
 */
@Api(tags = "学生控制器")
@RestController
@RequestMapping("/sms/studentController")
public class StudentController {

    @Autowired
    private StudentService studentService;


    // /sms/studentController/delStudentById 删除学生信息
    @ApiOperation("删除单个或者多个学生信息")
    @DeleteMapping("/delStudentById")
    public Result delStudentById(
            @ApiParam("要删除的学生编号JSON格式") @RequestBody List<Integer> ids
            ){
        studentService.removeByIds(ids);
        return Result.ok();
    }


    @ApiOperation("保存或者修改学生信息")
    @PostMapping("/addOrUpdateStudent")
    // sms/studentController/addOrUpdateStudent
    public Result addOrUpdateStudent(
            @ApiParam("保存或者修改的学生JSON") @RequestBody Student student
    ){
        Integer id = student.getId();
        if (null == id || 0 == id){
            student.setPassword(MD5.encrypt(student.getPassword()));
        }

        studentService.saveOrUpdate(student);

        return Result.ok();


    }


    //sms/studentController/getStudentByOpr/1/3?name= &clazzName=
    @ApiOperation("查询学生信息,分页带条件")
    @GetMapping("/getStudentByOpr/{pageNo}/{pageSize}")
    public Result getStudentsByOpr(
            @ApiParam("页码数") @PathVariable("pageNo")Integer pageNo,
            @ApiParam("页大小") @PathVariable("pageSize")Integer pageSize,
            @ApiParam("查询条件转换后端数据模型") Student student
    ){
        // 准备分页信息封装的page对象
        Page<Student> pageParam =new Page(pageNo,pageSize);
        // 获取分页的学生信息
        IPage<Student> studentPage = studentService.getStudentByOpr(pageParam, student);
        // 返回学生信息
        return Result.ok(studentPage);
    }
}
